﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection ;

namespace XDesigner.AppFramework.Actions
{
    public class AppReflectAction : AppAction
    {
        public static AppReflectAction Create(Type containerType , MethodInfo method , object instance)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (containerType == null)
            {
                throw new ArgumentNullException("containerType");
            }
            AppActionMethodAttribute attr = (AppActionMethodAttribute)Attribute.GetCustomAttribute(
                    method , typeof(AppActionMethodAttribute), false);
            if (attr == null)
            {
                return null ;
            }
            //System.Console.WriteLine( "+++++++" + m.Name);
            if (method.ReturnType.Equals(typeof(void)) == false)
            {
                return null ;
            }
            System.Reflection.ParameterInfo[] ps = method.GetParameters();
            if (ps == null || ps.Length != 2)
            {
                return null ;
            }
            if (ps[0].ParameterType.Equals(typeof(object))
                && ps[1].ParameterType.Equals(typeof(AppActionEventArgs)))
            {

                System.Delegate handler = null;
                if (method.IsStatic)
                {
                    handler = System.Delegate.CreateDelegate(typeof(AppActionEventHandler), method);
                }
                else if (instance != null)
                {
                    handler = System.Delegate.CreateDelegate(typeof( AppActionEventHandler ), instance, method);
                }
                string name = attr.Name ;
                //System.EventHandler h = new EventHandler( objInstance , m.Name );// m.MethodHandle );
                if (name == null || name.Trim().Length == 0)
                {
                    name = method.Name;
                }
                AppReflectAction action = new AppReflectAction();
                action.Name = attr.Name;
                action.Description = attr.Description;
                action.Shortcut = attr.Shortcut;
                action.Instance = instance;
                action.Handler = ( AppActionEventHandler ) handler;

                System.IO.Stream imgStream = null;

                string resource = attr.ImageResource ;
                if (resource == null || resource.Length == 0)
                {
                    imgStream = containerType.Assembly.GetManifestResourceStream(containerType.Namespace + resource);
                }
                if (imgStream == null)
                {
                    imgStream = containerType.Assembly.GetManifestResourceStream(containerType.Namespace + action.Name + ".bmp");
                }
                if (imgStream != null)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromStream(imgStream);
                    if (img is System.Drawing.Bitmap)
                    {
                        System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)img;
                        //bmp.MakeTransparent();
                        if (bmp.Width > 1 && bmp.Height > 1)
                        {
                            System.Drawing.Color c = bmp.GetPixel(0, bmp.Height - 1);
                            bmp.MakeTransparent(c);
                        }
                    }
                    action.Image = img;
                    imgStream.Close();
                }
                return action;
            }
            return null;
        }//public static AppReflectionAction Create()

        private object _Instance = null;

        public object Instance
        {
            get
            {
                return _Instance;
            }
            set
            {
                _Instance = value;
            }
        }

        private AppActionEventHandler _Handler = null;

        public AppActionEventHandler Handler
        {
            get
            { 
                return _Handler;
            }
            set
            { 
                _Handler = value;
            }
        }

        public override void BeforeExecute()
        {
            if (_Handler != null)
            {
                AppActionEventArgs args = new AppActionEventArgs(this.Framework, this, AppActionEventStyle.BeforeExecute);
                _Handler(this, args);
            }
        }

        public override void Execute()
        {
            if (_Handler != null)
            {
                AppActionEventArgs args = new AppActionEventArgs(this.Framework, this, AppActionEventStyle.Execute);
                _Handler( this, args );
            }
        }

        public override void AfterExecute()
        {
            if (_Handler != null)
            {
                AppActionEventArgs args = new AppActionEventArgs(this.Framework, this, AppActionEventStyle.AfterExecute );
                _Handler(this, args);
            }
        }
        
    }//public class AppReflectionAction : AppAction


    [System.AttributeUsage( AttributeTargets.Method )]
    public class AppActionMethodAttribute : Attribute
    {
        public AppActionMethodAttribute( string name )
        {
            _Name = name;
        }

        private string _Name = null;

        public string Name
        {
            get
            { 
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        private string _ImageResource = null;

        public string ImageResource
        {
            get
            { 
                return _ImageResource;
            }
            set
            {
                _ImageResource = value; 
            }
        }

        private string _Description = null;

        public string Description
        {
            get
            { 
                return _Description;
            }
            set
            { 
                _Description = value;
            }
        }


        private System.Windows.Forms.Shortcut intShortcut = System.Windows.Forms.Shortcut.None;
        /// <summary>
        /// 快捷键
        /// </summary>
        public virtual System.Windows.Forms.Shortcut Shortcut
        {
            get
            {
                return intShortcut;
            }
            set
            {
                intShortcut = value;
            }
        }
    }
}